/* ============================================ TechPower Electronics - Main JavaScript ============================================ */ document.addEventListener('DOMContentLoaded', function() { // Initialize all functions initHeader(); initMobileMenu(); initSmoothScroll(); initScrollToTop(); initAnimations(); initFormValidation(); initLanguageSelector(); }); /* ============================================ Header Scroll Effect ============================================ */ function initHeader() { const header = document.getElementById('header'); let lastScroll = 0; window.addEventListener('scroll', () => { const currentScroll = window.pageYOffset; // Add scrolled class for styling changes if (currentScroll > 50) { header.classList.add('scrolled'); } else { header.classList.remove('scrolled'); } lastScroll = currentScroll; }); } /* ============================================ Mobile Menu Toggle ============================================ */ function initMobileMenu() { const mobileToggle = document.getElementById('mobileToggle'); const navMenu = document.getElementById('navMenu'); const navLinks = document.querySelectorAll('.nav-link'); if (mobileToggle && navMenu) { mobileToggle.addEventListener('click', () => { mobileToggle.classList.toggle('active'); navMenu.classList.toggle('active'); // Animate hamburger const spans = mobileToggle.querySelectorAll('span'); if (mobileToggle.classList.contains('active')) { spans[0].style.transform = 'rotate(45deg) translate(5px, 5px)'; spans[1].style.opacity = '0'; spans[2].style.transform = 'rotate(-45deg) translate(7px, -6px)'; } else { spans[0].style.transform = 'none'; spans[1].style.opacity = '1'; spans[2].style.transform = 'none'; } }); // Close menu when clicking a link navLinks.forEach(link => { link.addEventListener('click', () => { mobileToggle.classList.remove('active'); navMenu.classList.remove('active'); const spans = mobileToggle.querySelectorAll('span'); spans[0].style.transform = 'none'; spans[1].style.opacity = '1'; spans[2].style.transform = 'none'; }); }); } } /* ============================================ Smooth Scroll for Navigation ============================================ */ function initSmoothScroll() { const links = document.querySelectorAll('a[href^="#"]'); links.forEach(link => { link.addEventListener('click', function(e) { const href = this.getAttribute('href'); if (href !== '#') { e.preventDefault(); const target = document.querySelector(href); if (target) { const headerHeight = document.querySelector('.header').offsetHeight; const targetPosition = target.getBoundingClientRect().top + window.pageYOffset - headerHeight; window.scrollTo({ top: targetPosition, behavior: 'smooth' }); } } }); }); } /* ============================================ Scroll to Top Button ============================================ */ function initScrollToTop() { const scrollTopBtn = document.getElementById('scrollTop'); window.addEventListener('scroll', () => { if (window.pageYOffset > 400) { scrollTopBtn.classList.add('visible'); } else { scrollTopBtn.classList.remove('visible'); } }); scrollTopBtn.addEventListener('click', () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }); } /* ============================================ Scroll Animations ============================================ */ function initAnimations() { // Intersection Observer for scroll animations const observerOptions = { root: null, rootMargin: '0px', threshold: 0.1 }; const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('animate-fade-in'); observer.unobserve(entry.target); } }); }, observerOptions); // Observe elements const animateElements = document.querySelectorAll( '.product-card, .factory-card, .cert-card, .client-card, .production-card, .feature-card' ); animateElements.forEach((el, index) => { el.style.opacity = '0'; el.style.transform = 'translateY(30px)'; el.style.transition = `all 0.6s ease ${index * 0.1}s`; observer.observe(el); }); // Add the animation class dynamically document.addEventListener('scroll', checkAnimations); } function checkAnimations() { const elements = document.querySelectorAll('.product-card, .factory-card, .cert-card, .client-card, .production-card, .feature-card'); elements.forEach((el, index) => { const rect = el.getBoundingClientRect(); const windowHeight = window.innerHeight; if (rect.top < windowHeight - 100) { el.style.opacity = '1'; el.style.transform = 'translateY(0)'; } }); } /* ============================================ Form Validation and Submission ============================================ */ function initFormValidation() { const form = document.getElementById('inquiryForm'); const formSuccess = document.getElementById('formSuccess'); if (form) { form.addEventListener('submit', function(e) { e.preventDefault(); // Validate form if (validateForm(this)) { // Show success message formSuccess.classList.add('show'); // Reset form form.reset(); // Hide success message after 5 seconds setTimeout(() => { formSuccess.classList.remove('show'); }, 5000); } }); } } function validateForm(form) { let isValid = true; const requiredFields = form.querySelectorAll('[required]'); requiredFields.forEach(field => { if (!field.value.trim()) { field.style.borderColor = '#ef4444'; isValid = false; } else { field.style.borderColor = '#E2E8F0'; } // Email validation if (field.type === 'email' && field.value) { const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailPattern.test(field.value)) { field.style.borderColor = '#ef4444'; isValid = false; } } }); return isValid; } /* ============================================ Language Selector ============================================ */ function initLanguageSelector() { const languageSelect = document.getElementById('languageSelect'); if (languageSelect) { languageSelect.addEventListener('change', function() { // In a real implementation, this would switch languages // For demo purposes, we'll just log the selection console.log('Language selected:', this.value); // Show a brief message const message = document.createElement('div'); message.className = 'language-message'; message.textContent = 'Multi-language version coming soon!'; message.style.cssText = ` position: fixed; top: 100px; left: 50%; transform: translateX(-50%); background: var(--primary); color: white; padding: 15px 30px; border-radius: 10px; z-index: 10000; animation: fadeInUp 0.3s ease; `; document.body.appendChild(message); setTimeout(() => { message.style.opacity = '0'; setTimeout(() => message.remove(), 300); }, 2000); }); } } /* ============================================ Product Category Filter (Optional Enhancement) ============================================ */ function filterProducts(category) { const products = document.querySelectorAll('.product-card'); products.forEach(product => { if (category === 'all' || product.dataset.category === category) { product.style.display = 'block'; } else { product.style.display = 'none'; } }); } /* ============================================ Video Fallback ============================================ */ function handleVideoFallback() { const video = document.querySelector('.hero-video'); if (video) { video.addEventListener('error', function() { // Fallback to gradient background if video fails const hero = document.querySelector('.hero'); hero.style.background = 'linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%)'; }); } } /* ============================================ Active Navigation Link on Scroll ============================================ */ function initActiveNavLink() { const sections = document.querySelectorAll('section[id]'); const navLinks = document.querySelectorAll('.nav-link'); window.addEventListener('scroll', () => { let current = ''; sections.forEach(section => { const sectionTop = section.offsetTop; const sectionHeight = section.clientHeight; if (scrollY >= sectionTop - 200) { current = section.getAttribute('id'); } }); navLinks.forEach(link => { link.classList.remove('active'); if (link.getAttribute('href') === `#${current}`) { link.classList.add('active'); } }); }); } // Initialize active nav link initActiveNavLink(); /* ============================================ Loading Animation ============================================ */ window.addEventListener('load', function() { // Add loaded class to body for any loading animations document.body.classList.add('loaded'); // Hide any preloader if exists const preloader = document.querySelector('.preloader'); if (preloader) { preloader.style.opacity = '0'; setTimeout(() => preloader.remove(), 300); } }); /* ============================================ Console Welcome Message ============================================ */ console.log(` ╔═══════════════════════════════════════════════════════════╗ ║ ║ ║ Welcome to TechPower Electronics Website ║ ║ B2B 3C Electronics Manufacturer & Supplier ║ ║ ║ ║ Contact: sales@techpower-electronics.com ║ ║ ║ ╚═══════════════════════════════════════════════════════════╝ `);